package MULE.Model;

import java.util.EnumSet;

import MULE.Model.Panel.NotEnoughMoneyException;

/**
 * This class creates a Store where player can buy and sell resources
 * 
 * 
 * @ author Anastasiya Cotton @ version 1.0 11.01.2013
 */
public class Store {

	private static Inventory inventory = new Inventory();

	/**
	 * Calculates price of a resource
	 * 
	 * @param Resource
	 *            .
	 * @param amount
	 * @return total cost of resources.
	 */
	public static Inventory cost(Resource type, int amount) {
		Inventory cost = new Inventory();
		int money = type.getValue();
		int total = money * amount;
		cost.add(Resource.MONEY, total);
		return cost;
	}

	/**
	 * Allows the given player to purchase given resource at specified cost.
	 * 
	 * @param p
	 *            The purchasing player.
	 * @param type
	 *            The resource being purchased.
	 * @param amount
	 *            The amount of the resource.
	 * @throws NotEnoughMoneyException
	 */
	public static void buy(Player p, Resource type, int amount)
			throws NotEnoughMoneyException {
		if (checkMoney(p, type, amount)) {
			p.getInventory().add(type, amount); // add resource to player's
												// inventory
			Inventory cost = cost(type, amount); // calc total price for the
													// resources
			p.getInventory().subtract(cost); // subtract money (total price)
												// from player
			inventory.add(cost); // add money to store
			inventory.subtract(type, amount); // remove resources from
												// store
		} else {
			throw new NotEnoughMoneyException();
		}
	}

	/**
	 * Allows the given player to sell given resources at specified price.
	 * 
	 * @param p
	 *            The selling player.
	 * @param type
	 *            The resource being sold.
	 * @param amount
	 *            The amount of the resource.
	 * @throws NotEnoughResourcesException
	 */
	public static void sell(Player p, Resource type, int amount) {
		if (checkInventory(p, type, amount)) {
			p.getInventory().subtract(type, amount); // remove resources from
														// player's inventory
			Inventory cost = cost(type, amount); // calc total price for the
													// resources
			p.getInventory().add(cost); // add money (total price) to player
			inventory.add(type, amount); // add resources to store's
											// inventory
			inventory.subtract(cost); // remove money from store
		} else {
			System.out.println("Not enough resources");
		}
	}

	/**
	 * Allows the given player to sell given resources at specified price.
	 * 
	 * @param p
	 *            The selling player.
	 * @param type
	 *            The resource being sold.
	 * @param amount
	 *            The amount of the resource.
	 */

	public static boolean checkMoney(Player p, Resource type, int amount) {
		int totalPrice = amount * type.getValue();
		if (totalPrice <= p.getMoney()) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Checks if the given player has enough resources to cell to the store.
	 * 
	 * @param p
	 *            The selling player.
	 * @param type
	 *            The resource being sold.
	 * @param amount
	 *            The amount of the resource.
	 * @return boolean true or false
	 */
	public static boolean checkInventory(Player p, Resource type, int amount) {
		if (amount <= p.getInventory().getAmount(type)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Checks if the store's inventory has specified resources.
	 * 
	 * @param type
	 *            Type of resource.
	 * @param amount
	 *            Amount of resource.
	 * @return True if the store has the resources, false otherwise.
	 */
	public static boolean checkStorehouse(Resource type, int amount) {
		if (inventory.getAmount(type) >= amount) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Gets the Store's inventory.
	 * 
	 * @return Store's inventory.
	 */
	public static Inventory getStorehouse() {
		return inventory;
	}

	/**
	 * Sets the Store's inventory.
	 * 
	 * @param Store
	 *            's inventory.
	 */
	public static void setStorehouse(Inventory storehouse) {
		inventory = storehouse;
	}

	// DEBUG purposes (no initial resources)
	public static void generateResources() {
		int bonus = 10;
		for (Resource resource : EnumSet.allOf(Resource.class)) {
			inventory.add(resource, bonus);
		}
	}
}
